<?php

class Message_model extends Model {
	 
   /**
	* Constructor 
	*
	*/
    function Messages_model()
    {
        parent::Model();
    }

    function addMessage($insertData=array())
    {
        try{
            return $this->db->insert('message', $insertData);
        }catch (Exception $e){
            throw $e;
            return false;
        }
    }

    function updateMessage($conditions = array(), $updateData=array(), $where_in = array())
    {
        try{
            if(count($conditions)>0 && is_array($conditions)){
                $this->db->where($conditions);
            }

            if(count($where_in) > 0 && is_array($where_in)){
                $this->db->where_in('message.id', $where_in);
            }

            return $this->db->update('message', $updateData);
        }catch (Exception $e){
            throw $e;
            return false;
        }
    }


	 function getMessageSend($conditions=array(),$fields='',$like=array(),$limit=array(), $orderby = array())
	 {
	 	//Check For Conditions
	 	if(count($conditions)>0 && is_array($conditions))		
	 		$this->db->where($conditions);
		
		if(is_array($like) and count($like)>0)
			$this->db->like($like);	
		
		//Check For Limit	
		if(is_array($limit))		
		{
			if(count($limit)==1)
	 			$this->db->limit($limit[0]);
			else if(count($limit)==2)
				$this->db->limit($limit[0],$limit[1]);
		}

         if(is_array($orderby) and count($orderby)>0) {
             $this->db->order_by($orderby[0], $orderby[1]);
         }else{
             $this->db->orderby('message.created','desc');
         }

        $this->db->from('message');
		$this->db->join('users', 'users.id = message.to_id','left');

		//Check For Fields	 
		if($fields!='')
				$this->db->select($fields);
		else 		
	 		$this->db->select('message.id,message.from_id,message.to_id,message.subject, message.message, message.created, message.status,users.user_name,users.logo');
			
		$result = $this->db->get();
		//print_r($result);
		return $result;
		
	 }
    function getMessageInbox($conditions=array(),$fields='',$like=array(),$limit=array(), $orderby = array(), $group_by = null)
    {
        //Check For Conditions
        if (count($conditions) > 0 && is_array($conditions))
            $this->db->where($conditions);

        if (is_array($like) and count($like) > 0)
            $this->db->like($like);

        //Check For Limit
        if (is_array($limit)) {
            if (count($limit) == 1)
                $this->db->limit($limit[0]);
            else if (count($limit) == 2)
                $this->db->limit($limit[0], $limit[1]);
        }

        if (is_array($orderby) and count($orderby) > 0) {
            $this->db->order_by($orderby[0], $orderby[1]);
        } else {
            $this->db->orderby('message.created', 'desc');
        }

        $this->db->from('message');
        $this->db->join('users', 'users.id = message.from_id', 'left');

        if (is_array($group_by)) {
           $this->db->group_by($group_by);
        }
        //Check For Fields
        if($fields!='')
            $this->db->select($fields);
        else
            $this->db->select('message.id,message.from_id,message.to_id,message.subject, message.message, message.created, message.status,users.user_name,users.logo, count(message.id) AS num_message');

        $result = $this->db->get();
        //print_r($result);
        return $result;

    }

}